﻿using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using NetMicro.EventBus.Internal;
using NetMicro.EventBus.Persistences;

namespace NetMicro.EventBus.BackgroundServices
{
    public class EventBusStartup : IHostedService
    {
        public EventBusStartup(
            IEventHandlerFindProvider eventHandlerFindProvider,
            IEventSubscriber eventSubscriber,
            IHostedStopToken hostedStopToken,
            IMessageRetryServiceProvider messageRetryServiceProvider,
            IEventDelayMessageServiceProvider eventDelayMessageServiceProvider,
            IMessageStorageInitializer messageStorageInitializer,
            IMessageFixServiceProvider messageFixServiceProvider)
        {
            EventHandlerFindProvider = eventHandlerFindProvider;
            EventSubscriber = eventSubscriber;
            HostedStopToken = hostedStopToken;
            MessageRetryServiceProvider = messageRetryServiceProvider;
            EventDelayMessageServiceProvider = eventDelayMessageServiceProvider;
            MessageStorageInitializer = messageStorageInitializer;
            MessageFixServiceProvider = messageFixServiceProvider;
        }


        private IEventHandlerFindProvider EventHandlerFindProvider { get; }
        private IEventSubscriber EventSubscriber { get; }

        private IHostedStopToken HostedStopToken { get; }

        private IMessageRetryServiceProvider MessageRetryServiceProvider { get; }

        private IEventDelayMessageServiceProvider EventDelayMessageServiceProvider { get; }

        private IMessageStorageInitializer MessageStorageInitializer { get; }

        private IMessageFixServiceProvider MessageFixServiceProvider { get; }

        public async Task Build()
        {
            //初始化数据库
            await MessageStorageInitializer.InitializeAsync(HostedStopToken.StopCancellationToken).ConfigureAwait(false);
            //消息重试后台任务
            await MessageRetryServiceProvider.Execute(HostedStopToken.StopCancellationToken).ConfigureAwait(false);
            //延迟任务后台任务
            await EventDelayMessageServiceProvider.Execute(HostedStopToken.StopCancellationToken).ConfigureAwait(false);
            //修复延迟任务发布失败产生锁无法解锁问题
            await MessageFixServiceProvider.DelayMessageExcute(HostedStopToken.StopCancellationToken).ConfigureAwait(false);
            //修复重试消息异常消息无法解锁的问题
            await MessageFixServiceProvider.RetryMessageExcute(HostedStopToken.StopCancellationToken).ConfigureAwait(false);
            //修复死信消息堆积问题
            await MessageFixServiceProvider.DeadLetterMessageExcute(HostedStopToken.StopCancellationToken).ConfigureAwait(false);

            // 加载所有的事件处理类
            System.Collections.Generic.IEnumerable<EventHandlerDescriptor> descriptors = EventHandlerFindProvider.FindAll();

            // 注册事件订阅
            foreach (EventHandlerDescriptor eventHandlerDescriptor in descriptors)
                await EventSubscriber.SubscribeAsync(eventHandlerDescriptor, HostedStopToken.StopCancellationToken).ConfigureAwait(false);
        }

        public async Task StartAsync(CancellationToken _)
        {
            await Build().ConfigureAwait(false);
        }

        public Task StopAsync(CancellationToken _)
        {
            (HostedStopToken as InternalHostedStopToken)!.Cancel();
            return Task.CompletedTask;
        }
    }
}